package zhtt.demo01;

import org.hibernate.LockMode;
import org.junit.Test;
import zhtt.JunitTestUtil;

/**
 * Created by zhtt on 2017/4/14.
 */
public class JunitTest01 extends JunitTestUtil{
    @Test
    public void transaction_save(){
        Student student=new Student("naem1",30,40);
        System.out.println(student);
        session.save(student);
        System.out.println(student);
    }

    @Test
    public void transaction_update(){
        Student student=new Student("naem1",30,40);
        System.out.println(student);
        session.save(student);
        System.out.println(student);
        student.setName("name2");//会报错，因为是只读的
        System.out.println(student);
    }

    @Test
    /**
     * 悲观锁     */
    public void persimisticlock(){
        Student student=session.get(Student.class,1, LockMode.READ);//
        System.out.println("student："+student);
        Student student2=session.get(Student.class,1, LockMode.UPGRADE_NOWAIT);//从后台运行的 SQL 中可以看到，多了 for update，即，使用了排他锁。
        System.out.println("student2："+student2);
    }
}
